import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Random;

public class PriorityQueueDemo {
    public static void main(String[] args) {
        // 优先队列自然排序示例
        Queue<Integer> integerQueue = new PriorityQueue<>(7);
        Random rand = new Random();
        for (int i = 0; i < 7; i++) {
            integerQueue.add(rand.nextInt(100));
        }
        for (int i = 0; i < 7; i++) {
            Integer in = integerQueue.poll();
            System.out.println("Processing Integer: " + in);
        }

        // 优先队列使用示例
        Queue<Customer> customerQueue = new PriorityQueue<>(7, idComparator);
        addDataToQueue(customerQueue);
        pollDataFromQueue(customerQueue);
    }

    // 匿名Comparator实现
    public static Comparator<Customer> idComparator = new Comparator<Customer>() {
        @Override
        public int compare(Customer o1, Customer o2) {
            return o2.getId() - o1.getId();
        }
    };

    // 用于往队列中增加数据的通用方法
    private static void addDataToQueue(Queue<Customer> customerQueue) {
        Random rand = new Random();
        for (int i = 0; i < 7; i++) {
            int id = rand.nextInt(100);
            customerQueue.add(new Customer(id, "tengyft " + id));
        }
    }

    // 用于从队列中取数据的通用方法
    private static void pollDataFromQueue(Queue<Customer> customerQueue) {
        while (true) {
            Customer element = customerQueue.poll();
            if (element == null) {
                break;
            }
            System.out.println("Processing Customer with ID = " + element.getId());
        }
    }
}
